package wnhz.com.business.groupshop.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.groupshop.bean.HotSeachBean;
import wnhz.com.business.netcallback.StringDialogCallback;
import wnhz.com.business.rebateshop.bean.HomeGoodsBean;
import wnhz.com.business.uitls.CommomDialog;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

/**
 * Created by Administrator on 2017/9/13 0013.
 * 搜索——全部
 */

public class SearchAllFragment extends BaseFragment {

    @BindView(R.id.recycler_head)
    RecyclerView recyclerHead;
    @BindView(R.id.recycler_record)
    RecyclerView recyclerRecord;
    Unbinder unbinder;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    private View view;
    private HotSeachBean hotSeachBean;
    private List<HotSeachBean> itemhand = new ArrayList<>();
    private List<HotSeachBean> itemhands = new ArrayList<>();
    private final int TOST = 1;
    public static SearchAllFragment newInstance() {
        return new SearchAllFragment();
    }



    private Handler mhandler =new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                //跳弹窗
                case TOST:
                    new CommomDialog(getContext(), R.style.dialog, "您确定清除历史搜索吗？", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm==true)
                            {
                                dialog.dismiss();
                                itemhands.clear();
                            }else
                            {
                                dialog.dismiss();
                            }

                        }
                    }).show();
                    break;
            }

        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_all, container, false);
        unbinder = ButterKnife.bind(this, view);
        loadData();
        return view;
    }

    private void loadData() {
        Map<String, String> map = new HashMap<>();
        if (MyApplication.getInstance().getInfoBean() == null){
            MyApplication.getInstance().BackLoginActivity();
            return;
        }
        /**
         * 热搜数据
         @param   token       用户token
         @param   goods_type  1.拼团  2.返利  3.免费
         */
        map.put("token", MyApplication.getInstance().getInfoBean().getToken());
        map.put("goods_type","2");//1.拼团  2.返利  3.免费
        OkGo.<String>post(Url.INDEX_SEARCHLIST)
                .tag(this)
                .params(map,false)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("==返利商城-热搜数据==",response.body().toString());
                        try {
                            JSONObject jsonObject  = new JSONObject(response.body());
                            String re = jsonObject.optString("re");
                            if (re.equals("1")){
                                Gson gson = new Gson();

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        setRecyclerHead();
        setRecyclerRecord();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initview() {
        recyclerHead.setLayoutManager(new GridLayoutManager(getContext(), 4, LinearLayoutManager.VERTICAL, false));
        recyclerRecord.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        itemhand.clear();
        itemhand.add(new HotSeachBean("暖宝宝"));
        itemhand.add(new HotSeachBean("牛皮糖"));
        itemhand.add(new HotSeachBean("手表"));
        itemhand.add(new HotSeachBean("手环"));
        itemhand.add(new HotSeachBean("男包"));
        itemhand.add(new HotSeachBean("手机壳"));
        itemhand.add(new HotSeachBean("花伞"));
        itemhand.add(new HotSeachBean("五娘液"));

        itemhands.add(new HotSeachBean("啤酒"));
        itemhands.add(new HotSeachBean("手链"));
        itemhands.add(new HotSeachBean("啤酒"));
    }

    private void setRecyclerHead() {
        recyclerHead.setAdapter(new BaseRVAdapter(getContext(), itemhand) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_hotseach_head;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.getTextView(R.id.tv_name).setText(itemhand.get(position).getName());
            }

        });
    }

    private void setRecyclerRecord() {
        recyclerRecord.setAdapter(new BaseRVAdapter(getContext(), itemhands) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_record;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.getTextView(R.id.tv_name).setText(itemhand.get(position).getName());
            }

        });
    }


    @OnClick(R.id.tv_clear)
    public void onViewClicked() {
        mhandler.sendEmptyMessage(TOST);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
